// miniprogram/pages/shop/shop.js
const db = wx.cloud.database(); //初始化数据库
const app = getApp();
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		shopid: 0,
		shop: [],
		menu: [],
		showNotice: false,
		count: '',
		sum: 0,
		activeKey: 0,
		toView: '',
		sumitPrice: 0,
		sumitText: '',
		sumitDisable: 'true',
		iconColor: '#b3b3b3',
		shoppingListShow: false,
		// 展开购物车
		shoppingList: [],
		totalBale: 0,
		quantity: 0,
		// 商品分类
		menuType: []
	},
	// 提交订单
	submitList: function() {
		// app.globalData.shoppingList = this.data.shoppingList
		//  
		//	传入店铺id
		let shopId = this.data.shop._id
		// 传入商品（需要转为JSON）
		let shopcar = JSON.stringify(this.data.shoppingList)
		let sumitPrice = this.data.sumitPrice
		console.log(shopcar)
		wx.showLoading({
			title: '订单生成中',
		})
		setTimeout(function() {
			wx.navigateTo({
				url: `../submit/submit?shopId=${shopId}&shopcar=${shopcar}&sumitPrice=${sumitPrice}`
			})
			wx.hideLoading()
		}, 300)
	},
	// 展开购物车
	showShopping: function() {
		// console.log(!this.data.shoppingListShow)
		if (!this.data.shoppingListShow) {
			this.setData({
				shoppingListShow: true
			})
		} else this.closeShoppingList()
	},
	closeShoppingList: function() {
		this.setData({
			shoppingListShow: false
		})
	},
	showNotice: function() {
		this.setData({
			showNotice: true
		})
	},
	closeNotice: function() {
		this.setData({
			showNotice: false
		})
	},
	stickyChange: function(event) {
		let target = event.currentTarget.dataset.opt;
		this.setData({
			toView: target
		})
	},
	quantityChange: function(e) {
		// 商品
		let item = e.currentTarget.dataset.item
		// 商品名
		let name = item.name
		// 购物车列表
		let shoppingList = this.data.shoppingList
		// 商品名列表
		let nameList = []
		shoppingList.forEach((item, index) => {
			nameList.push(item.name)
		})
		// console.log(event.detail)
		// console.log(item)
		// 步进器：event.detail为数量
		if (e.detail > 0) {
			// console.log(item)
			// console.log(shoppingList)
			item.count = e.detail
			// 同名商品数量+1，不同商品加入列表
			if (nameList.indexOf(name) == -1) {
				shoppingList.push(item)
			} else {
				shoppingList[nameList.indexOf(name)].count = e.detail
			}
		} else {
			// 删除商品
			shoppingList.forEach((item, index) => {
				if (name == item.name) shoppingList.splice(index, 1)
			})
		}
		this.setData({
			shoppingList: shoppingList
		})
		// console.log(this.data.shoppingList)
	},
	quantityPlus: function(event) {
		let price = parseInt((parseInt(event.currentTarget.dataset.price) + parseInt(event.currentTarget
			.dataset.bale)).toString() + '00');
		this.setData({
			sum: this.data.sum + 1,
			count: this.data.sum + 1,
			iconColor: '#1989fa',
			sumitPrice: this.data.sumitPrice + price,
			totalBale: this.data.sum + parseInt(event.currentTarget.dataset.bale),
		})
		if (this.data.sumitPrice >= this.data.shop.startPrice) {
			this.setData({
				sumitText: '结算',
				sumitDisable: false
			})
		} else {
			this.setData({
				sumitText: '￥' + this.data.shop.startPrice + ' 起送',
				sumitDisable: true
			})
		}
	},
	quantityMinus: function(event) {
		let price = parseInt((parseInt(event.currentTarget.dataset.price) + parseInt(event.currentTarget
			.dataset.bale)).toString() + '00')
		this.setData({
			sum: this.data.sum - 1,
			count: this.data.sum - 1,
			sumitPrice: this.data.sumitPrice - price,
			totalBale: this.data.sum - parseInt(event.currentTarget.dataset.bale),
		})
		if (this.data.sum == 0) {
			this.setData({
				count: '',
				iconColor: '#b3b3b3'
			})
		}
		if (this.data.sumitPrice >= this.data.shop.startPrice) {
			this.setData({
				sumitText: '结算',
				sumitDisable: false
			})
		} else {
			this.setData({
				sumitText: '￥' + this.data.shop.startPrice + ' 起送',
				sumitDisable: true
			})
		}
	},
	// 清空购物车
	clear: function() {
		wx.redirectTo({
			url: `../shop/shop?shopid=${this.data.shopid}`
		})
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log(options.shopid)
		// let shopId = 0
		// shopId = this.data.shopid != 0 ? this.data.shopid : options.shopid
		db.collection('shop').doc(options.shopid).get({
			success: (res) => {
				// console.log(res.data)
				// 提取商品分类
				let menu = res.data.menu
				let menuType = []
				console.log(menu)
				for (let key in menu) {
					// console.log(menu[key].type)
					// console.log(menuType.indexOf(menu[key].type))
					if (menuType.indexOf(menu[key].type) == -1) {
						menuType.push(menu[key].type)
					}
				}
				console.log(menuType)
				this.setData({
					menuType: menuType,
					shop: res.data,
					menu: menu,
					sumitText: '￥' + res.data.startPrice + ' 起送',
					shopid: options.shopid
				})
				// console.log(this.data.menu)
				console.log(this.data.shopid)
			}
		})

	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
